from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest,time

class Test_so(unittest.TestCase):

    def setUp(self):
        # 制定浏览器
        self.driver = webdriver.Firefox()
        # 打开网址
        self.driver.get('https://www.so.com/')

    def tearDown(self):
        time.sleep(3)
        # 退出浏览器
        self.driver.quit()

    def test_01(self):
        # 搜索框输入
        self.driver.find_element(By.ID,'input').send_keys('我在做软件测试')
        # 点击搜索按钮
        self.driver.find_element(By.ID,'search-button').click()
        dy = self.driver.find_element(By.XPATH,'/html/body/div[6]/div[1]/div[1]/div[2]/ul/li[1]/div[1]/span[2]/a/em[1]').text
        # 验证搜索结果
        self.assertIn('软件测试',dy)
        # 截图
        self.driver.save_screenshot('cs.png')
        # 输出搜索到的网站名称
        a = self.driver.find_elements(By.TAG_NAME,'a')
        print(a)
        # 下一页
        self.driver.find_element(By.XPATH,'/html/body/div[6]/div[3]/div[1]/a[1]').click()
        # 输出搜索到的网站名称
        a1 = self.driver.find_elements(By.TAG_NAME, 'a')
        print(a1)

    def test_02(self):
        self.driver.find_element(By.NAME,'q').send_keys('我在做软件测试')